package top.infopub.mgr.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class OAFlowUtil {
//    private static String oaToDoUrl = "http://proxy.zhongnangroup.cn/znapicloud/api/jingshi/erp/todo";
    private static String oaToDoUrl = "http://proxy.zhongnangroup.cn/znservice/erpoa/inserterpoa";

//    private static String oaFinishUrl = "http://proxy.zhongnangroup.cn/znapicloud/api/jingshi/erp/finish";
    private static String oaFinishUrl = "http://proxy.zhongnangroup.cn/znservice/erpoa/updateerpoa";

    public static String sendOAToDo(String title, String sendUserCode, String sendUserName, String toUserCode, String toUserName, String companyName, String pageFlag, String id, String typeId, String type, String status) {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/#/";
        String basePath = "https://ssjcs.ninewe.com/#/";
        StringBuffer backUrl = new StringBuffer();
        backUrl.append(basePath);
        backUrl.append("?pageFlag=");
        backUrl.append(pageFlag);
        backUrl.append("&id=");
        backUrl.append(id);
        backUrl.append("&t=");
        backUrl.append(System.currentTimeMillis());
        if (StringUtils.isNotBlank(typeId)) {
            backUrl.append("&typeId=");
            backUrl.append(typeId);
        }
        if (StringUtils.isNotBlank(type)) {
            backUrl.append("&type=");
            backUrl.append(type);
        }
        if (StringUtils.isNotBlank(status)) {
            backUrl.append("&status=");
            backUrl.append(status);
        }
//        System.out.println(backUrl.toString());
        Map<String, String> oaMap = new HashMap<>();
        oaMap.put("name", title);  //待办标题
        oaMap.put("createname", sendUserName);  //创建人名称
        oaMap.put("createdate", DateUtils.currentDateDef());  //创建时间，格式：yyyy-MM-dd HH:mm:ss
//        oaMap.put("type", "ZNJC0526");
        oaMap.put("type", "znjt");  //固定值，OA分配
        oaMap.put("empid", "0001B210000000FDZ944");  //待办人NCID
        oaMap.put("username", toUserCode);  //待办人工号
        oaMap.put("sendusername", toUserName);  //待办人姓名
        oaMap.put("url", backUrl.toString());  //处理公文页面链接
        oaMap.put("company", companyName);  //公司名称
        oaMap.put("staffcode", toUserCode);  //同username
//        oaMap.put("phoneUrl", "");  //手机端处理地址
//        String resp = HttpClientUtil.postJson(oaToDoUrl, JSON.toJSONString(oaMap));
//        String resp1 = HttpClientUtil.postBody(oaToDoUrl, JSON.toJSONString(oaMap));
        String resp = HttpClientUtil.httpClientGet(oaToDoUrl, null, oaMap);
        String oaId = "";
        if (StringUtils.isNotBlank(resp)) {
            JSONObject str = JSON.parseObject(resp);
            String code = str.getString("code");
            if (StringUtils.equals("1", code)) {
                oaId = str.getString("id");
            }
        }
        return oaId;
    }

    public static boolean finishOA(String id) {
        String url = oaFinishUrl + "?id=" + id;
        String resp = HttpClientUtil.httpClientGet(url);
        if (StringUtils.isNotBlank(resp)) {
            JSONObject str = JSON.parseObject(resp);
            String code = str.getString("code");
            if (StringUtils.equals("1", code)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
//        String oaId = sendOAToDo("测试", "1937694", "王文波", "1937694", "王文波", "中南建筑", "2", "68", null, "7", null);
//        String oaId = sendOAToDo("测试", "0", "监察", "1937694", "王文波", "中南集团", "1", "cdbeb3edee6fff7e89e9f472dc36c09b", "1", null, "1");
//        System.out.println(oaId);
//        System.out.println(TokenMD5Utils.pwdMD5(Constants.OA_TOKEN_SALT_VALUE, "1937694"));
//        System.out.println(TokenMD5Utils.pwdMD5("znjt", "1937694"));
        //https://ssjcs.ninewe.com/#/?pageFlag=2&id=68&type=7&token=1937694_f017a2495497ffc6911e757163fac105
        //https://ssjcs.ninewe.com/#/?pageFlag=1&id=961daa4885db2f40e06ca96077b43620&typeId=1&status=2&token=1937694_f017a2495497ffc6911e757163fac105
        //{"code":"1","id":"de99aac8554a6ea2e3b82fee68b710d5","msg":"提交成功"}
        //f1976bb2ae6e02e7ff15d786be6558dc
        //f1976bb2ae6e02e7ff15d786be6558dc
        finishOA("dbbc2a8f51a9c98ff59b2bc6af928a2b");
    }
}
